/**
 * @File SegmentSummaryDaoImpl.java
 * @author Dheeraj Singh
 * @Version 1.0
 * @Date June 22, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */

package in.ac.dei.edrp.purchase.flexFieldSettings.segmentSummary;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapClient;

@Component
@Qualifier("SegmentSummaryDaoImpl")
public class SegmentSummaryDaoImpl extends SqlMapClientDaoSupport implements SegmentSummaryDao{

	private static Logger logObj = Logger.getLogger(SegmentSummaryDaoImpl.class);
	
	@Autowired
	public SegmentSummaryDaoImpl(SqlMapClient sqlMapClient){
		setSqlMapClient(sqlMapClient);
	}
	
	public List<SegmentSummaryBean> getAllSegments(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.getAllSegments",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public List<SegmentSummaryBean> getFindSegments(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.getFindSegments",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public List<SegmentSummaryBean> getFindColumns(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.getFindColumns",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public List<SegmentSummaryBean> getFindValueSets(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.getFindValueSets",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public boolean setSegment(SegmentSummaryBean segmentSummaryBean) {
		boolean flag = false;
		try{
			SegmentSummaryBean input = new SegmentSummaryBean();
			input = (SegmentSummaryBean) getSqlMapClient().queryForObject("segmentSummary.getMaxSegmentId", segmentSummaryBean);
			int segmentId = input.getSegmentId();
			segmentId++;
			segmentSummaryBean.setSegmentId(segmentId);
			getSqlMapClient().insert("segmentSummary.setSegment", segmentSummaryBean);
			flag = true;
		}catch(Exception ex){
			logObj.error(ex);
			flag = false;
		}
		return flag;
	}

	public String checkSequence(SegmentSummaryBean segmentSummaryBean) {
		String str = "";
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			if(segmentSummaryBean.getSegmentId() == 0){
				list = getSqlMapClient().queryForList("segmentSummary.checkSequence",segmentSummaryBean);
			}else{
				list = getSqlMapClient().queryForList("segmentSummary.checkSequenceForUpdate",segmentSummaryBean);
			}
			if(list.size() > 0){
				str = "Exists";
			}else{
				str = "Not Exists";
			}
		}catch(Exception ex){
			logObj.error(ex);
			str = "Exception";
		}
		return str;
	}

	public String checkSegmentColumn(SegmentSummaryBean segmentSummaryBean) {
		String str = "";
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			if(segmentSummaryBean.getSegmentId() == 0){
				list = getSqlMapClient().queryForList("segmentSummary.checkSegmentColumn",segmentSummaryBean);
			}else{
				list = getSqlMapClient().queryForList("segmentSummary.checkSegmentColumnForUpdate",segmentSummaryBean);
			}
			if(list.size() > 0){
				str = "Exists";
			}else{
				str = "Not Exists";
			}
		}catch(Exception ex){
			logObj.error(ex);
			str = "Exception";
		}
		return str;
	}

	public List<SegmentSummaryBean> findSegment(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.findSegment",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public boolean updateSegment(SegmentSummaryBean segmentSummaryBean) {
		boolean flag = false;
		try{
			getSqlMapClient().update("segmentSummary.updateSegment", segmentSummaryBean);
			flag = true;
		}catch(Exception ex){
			logObj.error(ex);
			flag = false;
		}
		return flag;
	}

	public List<SegmentSummaryBean> getQualifiers(
			SegmentSummaryBean segmentSummaryBean) {
		List<SegmentSummaryBean> list = null;
		try{
			list = new ArrayList<SegmentSummaryBean>();
			list = getSqlMapClientTemplate().queryForList("segmentSummary.getQualifiers",segmentSummaryBean);
		}catch(Exception ex){
			logObj.error(ex);
		}
		return list;
	}

	public boolean setQualifiers(SegmentSummaryBean segmentSummaryBean) {
		boolean flag = false;
		try{
			getSqlMapClient().update("segmentSummary.setQualifiers", segmentSummaryBean);
			flag = true;
		}catch(Exception ex){
			logObj.error(ex);
			flag = false;
		}
		return flag;
	}

}
